﻿using Sipek.Common;
using Sipek.Common.CallControl;
using Sipek.Sip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinformTest.Sip;

namespace WinformTest
{
    public partial class Form1 : Form
    {
        CCallManager CallManager
        {
            get { return CCallManager.Instance; }
        }

        IConfiguratorInterface PhoneConfig
        {
            get { return new PhoneConfig(); }
        }

        IStateMachine call;

        public Form1()
        {
            InitializeComponent();

            // Inject VoIP stack engine to CallManager
            CallManager.StackProxy = pjsipStackProxy.Instance;
            CallManager.Config = PhoneConfig;
            CallManager.CallStateRefresh += CallManager_CallStateRefresh;
            CallManager.IncomingCallNotification += CallManager_IncomingCallNotification;
            CallManager.Initialize();

            pjsipRegistrar.Instance.Config = PhoneConfig;
            pjsipRegistrar.Instance.AccountStateChanged += Instance_AccountStateChanged;

            pjsipStackProxy.Instance.Config = PhoneConfig;
            pjsipStackProxy.Instance.DtmfDigitReceived += Instance_DtmfDigitReceived;
            
            pjsipPresenceAndMessaging.Instance.Config = PhoneConfig;
            pjsipPresenceAndMessaging.Instance.MessageReceived += Instance_MessageReceived;

            CallManager.MediaProxy = new CMediaPlayerProxy(); 

        }

        private void CallManager_IncomingCallNotification(int sessionId, string number, string info)
        {
            MessageBox.Show("消息来了！");
        }

        private void Instance_MessageReceived(string from, string text)
        {
            MessageBox.Show("接收到消息！" + text);
        }

        private void Instance_DtmfDigitReceived(int callId, int digit)
        {
            MessageBox.Show("DTMF信息");
        }

        private void Instance_AccountStateChanged(int accountId, int accState)
        {
            MessageBox.Show("账户状态改变！"+ accState.ToString());
        }

        private void CallManager_CallStateRefresh(int sessionId)
        {
            MessageBox.Show("通话状态改变！"+ sessionId.ToString());
        }

        //注册
        private void button1_Click(object sender, EventArgs e)
        {
            pjsipRegistrar.Instance.registerAccounts();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            call = CallManager.createOutboundCall("8008");
        }
        
        private void OnRegistrationUpdate(int accountId, int accState)
        {
            MessageBox.Show("更新注册信息:" + accState.ToString());
        }

        private void OnStateUpdate(int sessionId)
        {
            var msg= CallManager.getCall(sessionId).StateId.ToString();
            MessageBox.Show("状态更新:" + msg);
        }
    }
}
